package com.yixun.show.service.impl;

import com.yixun.entity.DiQu;
import com.yixun.entity.Dict;
import com.yixun.entity.YxDept;
import com.yixun.lbx.entity.YxProBeeSwarm;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.entity.YxProProductInfo;
import com.yixun.lbx.entity.YxProProductYield;
import com.yixun.show.domain.ShowBean;
import com.yixun.show.mapper.ShowMapper;
import com.yixun.show.service.ShowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @author Mr_Wang
 */
@Service
public class ShowServiceImpl implements ShowService {

    @Autowired
    private ShowMapper showMapper;

    @Override
    public ShowBean getShowData() {

        //查询各个产品的总产量；开始；
        //查询所有产品分类；
        List<Dict> dictList = showMapper.findProductType();
        List<String> nameList = new ArrayList<>();
        List<Double> numLlist = new ArrayList<>();
        ShowBean showBean = new ShowBean();
        double num = 0 ;
        for (Dict dict : dictList) {
            nameList.add(dict.getDict_name());
            //根据分类查询产量信息；
            List<YxProProductYield> yxProProductYieldList = showMapper.findProductYield(dict.getDict_code());
            //判断产品数量；
            num = 0 ;
            if (yxProProductYieldList.size() > 1){
                for (YxProProductYield yxProProductYield : yxProProductYieldList) {
                    //统计累加产量
                    num = Double.valueOf(String.valueOf(BigDecimal.valueOf(num).add(BigDecimal.valueOf(yxProProductYield.getScVal()))));
                }
            } else {
                //统计累加产量
                num = yxProProductYieldList.get(0).getScVal();
            }
            numLlist.add(num);
        }
        showBean.setCp_names(nameList);
        showBean.setCp_cls(numLlist);
        //查询各个产品的总产量；结束；

        //查询蜂蜜来源；开始；
        //查询所有成分；百分比
        //蜂蜜来源名称；
        List<String> sourceNames = new ArrayList<>();
        //蜂蜜来源数据；
        List<Integer> sourceNums = new ArrayList<>();
        List<YxProProductInfo> yxProProductInfoList = showMapper.findComponent();
        if (yxProProductInfoList.size() > 0){
            for (YxProProductInfo yxProProductInfo : yxProProductInfoList) {
                sourceNames.add(yxProProductInfo.getComponent());
                sourceNums.add(showMapper.findComponentNum(yxProProductInfo.getComponent()));
            }
            showBean.setSourceNames(sourceNames);
            showBean.setSourceNums(sourceNums);
        } else {
            showBean.setSourceNames(sourceNames);
            showBean.setSourceNums(sourceNums);
        }

        //查询蜂蜜来源；结束；

        //蜂群结构开始
        //统计所有蜂群数量；
        int totalNum = showMapper.findBeeSwarmTotalNum();
        //强群数量；
        int strongGroupNum = 0 ;
        //中等群数量；
        int mediumGroupNum = 0 ;
        //弱群数量；
        int weakGroupNum = 0 ;
        //蜂群数量集合；
        List<Integer> beeSwarmNums = new ArrayList<>();
        //蜂群名称；
        List<String> beeSwarmNames = new ArrayList<>();
        //查询蜂群种类；
        DecimalFormat df=new DecimalFormat("0.00");
        List<YxProBeeSwarm> yxProBeeSwarmList = showMapper.findBeeSwarmType();
        if (yxProBeeSwarmList.size() > 0){
            for (YxProBeeSwarm yxProBeeSwarm : yxProBeeSwarmList) {
                //查询每个种类蜂群数量；
                switch (yxProBeeSwarm.getBeeType()){
                    case "强群":
                        beeSwarmNames.add(yxProBeeSwarm.getBeeType());
                        int x = showMapper.findBeeSwarmNum(yxProBeeSwarm.getBeeType());
                        beeSwarmNums.add(x);
                        showBean.setStrongGroupNum(x);
                        String format = String.valueOf(BigDecimal.valueOf(x).divide(BigDecimal.valueOf(totalNum), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)));
                        format += "%";
                        showBean.setStrongGroupNumProportion(format);
                        break;
                    case "中等群":
                        beeSwarmNames.add(yxProBeeSwarm.getBeeType());
                        int y = showMapper.findBeeSwarmNum(yxProBeeSwarm.getBeeType());
                        beeSwarmNums.add(y);
                        showBean.setMediumGroupNum(y);
                        String s1 = String.valueOf(BigDecimal.valueOf(y).divide(BigDecimal.valueOf(totalNum), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)));
                        s1 += "%";
                        showBean.setMediumGroupNumProportion(s1);
                        break;
                    case "弱群":
                        beeSwarmNames.add(yxProBeeSwarm.getBeeType());
                        int z = showMapper.findBeeSwarmNum(yxProBeeSwarm.getBeeType());
                        beeSwarmNums.add(z);
                        showBean.setWeakGroupNum(z);
                        String s3 = String.valueOf(BigDecimal.valueOf(z).divide(BigDecimal.valueOf(totalNum), 2, BigDecimal.ROUND_HALF_UP).multiply(BigDecimal.valueOf(100)));
                        s3 += "%";
                        showBean.setWeakGroupNumProportion(s3);
                        break;
                }
            }
            showBean.setBeeSwarmNames(beeSwarmNames);
            showBean.setBeeSwarmNums(beeSwarmNums);
        } else {
            showBean.setBeeSwarmNames(beeSwarmNames);
            showBean.setBeeSwarmNums(beeSwarmNums);
        }
        showBean.setTotalNum(totalNum);

        //蜂群结构结束

        //产品价格开始；
        //获取当前年份；
        int year = Calendar.getInstance().get(Calendar.YEAR);
        List<String> productNameList = new ArrayList<>();
        List<Double> fmPriceList = Arrays.asList(new Double[12]);
        List<Double> gmPriceList = Arrays.asList(new Double[12]);
        List<Double> hfPriceList = Arrays.asList(new Double[12]);
        List<Double> fjPriceList = Arrays.asList(new Double[12]);
        List<Double> fwjPriceList = Arrays.asList(new Double[12]);
        //组装数据展示的X轴；月份
        if (dictList.size() > 0){
            for (Dict dict : dictList) {
                productNameList.add(dict.getDict_name());
                //查询蜂产品中当前种类产品的相关数据；
                List<YxProProductInfo> yxProductInfos = showMapper.findProductPrice(year,dict.getDict_code());
                if (yxProductInfos.size() > 0){

                    for (int i = 0; i < yxProductInfos.size(); i++) {
                       switch (yxProductInfos.get(i).getMonth()){
                           case 1 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 2 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 3 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 4 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 5 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 6 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 7 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 8 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 9 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 10 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 11 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                           case 12 :
                               setPriceList(fmPriceList, gmPriceList, hfPriceList, fjPriceList, fwjPriceList, yxProductInfos, i);
                               break;
                       }
                    }
                }

            }


            for (int j = 0; j < 12; j++) {
                if (j == 0 && fmPriceList.get(j) == null) {
                    //去年
                    int oldYear = getOldYear();
//                    查询去年价格平均值
                    Double avgPrice = showMapper.findOldYearAvgPrice(oldYear,1);
                    fmPriceList.set(j,avgPrice);
//                    fmPriceList.set(j,0.0);
                }
                if (j == 0 && gmPriceList.get(j) == null) {
                    int oldYear = getOldYear();
                    Double avgPrice = showMapper.findOldYearAvgPrice(oldYear,2);
                    gmPriceList.set(j,avgPrice);
                }
                if (j == 0 && fjPriceList.get(j) == null) {
                    int oldYear = getOldYear();
                    Double avgPrice = showMapper.findOldYearAvgPrice(oldYear,3);
                    fjPriceList.set(j,avgPrice);
                }
                if (j == 0 && hfPriceList.get(j) == null) {
                    int oldYear = getOldYear();
                    Double avgPrice = showMapper.findOldYearAvgPrice(oldYear,4);
                    hfPriceList.set(j,avgPrice);
                }
                if (j == 0 && fwjPriceList.get(j) == null) {
                    int oldYear = getOldYear();
                    Double avgPrice = showMapper.findOldYearAvgPrice(oldYear,5);
                    fwjPriceList.set(j,avgPrice);
                }

                if (j >= 1 && fmPriceList.get(j) == null) {
                    fmPriceList.set(j,fmPriceList.get(j-1));
                }
                if (j >= 1 && gmPriceList.get(j) == null) {
                    gmPriceList.set(j,gmPriceList.get(j-1));
                }
                if (j >= 1 && fjPriceList.get(j) == null) {
                    fjPriceList.set(j,fjPriceList.get(j-1));
                }
                if (j >= 1 && hfPriceList.get(j) == null) {
                    hfPriceList.set(j,hfPriceList.get(j-1));
                }
                if (j >= 1 && fwjPriceList.get(j) == null) {
                    fwjPriceList.set(j,fwjPriceList.get(j-1));
                }
            }
        }
        showBean.setProductNameList(productNameList);
        showBean.setFmPriceList(fmPriceList);
        showBean.setGmPriceList(gmPriceList);
        showBean.setHfPriceList(hfPriceList);
        showBean.setFjPriceList(fjPriceList);
        showBean.setFwjPriceList(fwjPriceList);
        //产品价格结束；

        //企业分布开始；
        //查询数据字典企业分类；
        List<Dict> company = showMapper.findCompanyType();
        //查询行政区划编码；
        List<DiQu> dqList = showMapper.findDqCode();
        //地区名称集合；
        List<String> diQuNameList = new ArrayList<>();
        //企业分类名称集合；
        List<String> companyTypeList = new ArrayList<>();
        //加工企业数据集合；
        List<Integer> jgCompanyList = new ArrayList<>();
        //生产企业数据集合；
        List<Integer> scCompanyList = new ArrayList<>();
        //其他企业数据集合；
        List<Integer> qtCompanyList = new ArrayList<>();
        //销售企业数据集合;
        List<Integer> sellCompanyList = new ArrayList<>();
        if (dqList.size() > 0 ){
            for (DiQu diQu : dqList) {
                diQuNameList.add(diQu.getDq_name());
            }
        }
        if (company.size() > 0){
            for (Dict dict : company) {
                companyTypeList.add(dict.getDict_name());
                if (dqList.size() > 0){
                    switch (dict.getDict_name()){
                        case "生产企业":
                            for (DiQu diQu : dqList) {
                                scCompanyList.add(showMapper.findCompanyNum(dict.getDict_name(),diQu.getDq_id()));
                            }
                            break;
                        case "销售企业":
                            for (DiQu diQu : dqList) {
                                sellCompanyList.add(showMapper.findCompanyNum(dict.getDict_name(),diQu.getDq_id()));
                            }
                            break;
                        case "其他企业":
                            for (DiQu diQu : dqList) {
                                qtCompanyList.add(showMapper.findCompanyNum(dict.getDict_name(),diQu.getDq_id()));
                            }
                            break;
                        case "加工企业":
                            for (DiQu diQu : dqList) {
                                jgCompanyList.add(showMapper.findCompanyNum(dict.getDict_name(),diQu.getDq_id()));
                            }
                            break;
                    }

                }
            }
        }
        showBean.setDiQuNameList(diQuNameList);
        showBean.setSellCompanyList(sellCompanyList);
        showBean.setJgCompanyList(jgCompanyList);
        showBean.setScCompanyList(scCompanyList);
        showBean.setQtCompanyList(qtCompanyList);
        showBean.setCompanyTypeList(companyTypeList);
        //企业分布结束；

        //加工企业总数；
        int jgCompanyTotalNum = 0 ;
        //销售企业总数；
        int xsCompanyTotalNum = 0 ;
        //生产企业总数；
        int scCompanyTotalNum = 0 ;
        if (companyTypeList.size() > 0 ){
            for (String s : companyTypeList) {
                switch (s){
                    case "生产企业":
                        scCompanyTotalNum = showMapper.findCompanyTotalNum(s);
                        break;
                    case "销售企业":
                        xsCompanyTotalNum = showMapper.findCompanyTotalNum(s);
                        break;
                    case "加工企业":
                        jgCompanyTotalNum = showMapper.findCompanyTotalNum(s);
                        break;
                }
            }
        }
        showBean.setJgCompanyTotalNum(jgCompanyTotalNum);
        showBean.setXsCompanyTotalNum(xsCompanyTotalNum);
        showBean.setScCompanyTotalNum(scCompanyTotalNum);

        //总产量；
        showBean.setTotalOutput(showMapper.findTotalOutput());

        //查询所有企业；
        List<YxProCompany> companyList = showMapper.findAllCompanyList();
        //当该企业为生产企业时；获取蜂箱数量；
        if (companyList.size() > 0){
            for (YxProCompany yxProCompany : companyList) {
                if ("生产企业".equals(yxProCompany.getCompanyType())){
                    //查询蜂箱数量；
                    int i = showMapper.findBeeHiveNum(yxProCompany.getQyId());
                    yxProCompany.setBeeHiveNum(i);
                }
            }
        }

        //产值计算；
        double fm_total_price = 0.0 ;
        double fwj_total_price = 0.0 ;
        double total_price = 0.0 ;
        if (nameList.size() > 0 ){
            for (int i = 0; i < nameList.size() ; i++) {

                switch (nameList.get(i)){
                    case "蜂蜜":
                        fm_total_price =  Double.valueOf(String.valueOf(BigDecimal.valueOf(numLlist.get(i)).multiply(BigDecimal.valueOf(Double.valueOf(110.00)))));
                        break;
                    case "蜂王浆" :
                        fwj_total_price =  Double.valueOf(String.valueOf(BigDecimal.valueOf(numLlist.get(i)).multiply(BigDecimal.valueOf(Double.valueOf(220.00)))));
                        break;
                }
            }
        }

        total_price = Double.valueOf(String.valueOf(BigDecimal.valueOf(fm_total_price).add(BigDecimal.valueOf(fwj_total_price))));
        total_price = Double.valueOf(String.valueOf(BigDecimal.valueOf(total_price).divide(BigDecimal.valueOf(10000.00))));


        showBean.setCompanyList(companyList);
        showBean.setTotalPrice(total_price);



        return showBean;
    }

    //查询企业简介
    @Override
    public YxProCompany getQyData(String brand) {
        YxProCompany company = showMapper.getQyData(brand);
        return company;
    }







    private int getOldYear() {
        int i= Calendar.getInstance().get(Calendar.YEAR);
        return i-1;
    }


    //产品价格方法；
    private void setPriceList(List<Double> fmPriceList, List<Double> gmPriceList, List<Double> hfPriceList, List<Double> fjPriceList, List<Double> fwjPriceList, List<YxProProductInfo> yxProductInfos, int i) {
        switch (yxProductInfos.get(i).getProType()){
            case "1":
                setPrice(fmPriceList, yxProductInfos, i);
                break;
            case "2":
                setPrice(gmPriceList, yxProductInfos, i);
                break;
            case "3":
                setPrice(fjPriceList, yxProductInfos, i);
                break;
            case "4":
                setPrice(hfPriceList, yxProductInfos, i);
                break;
            case "5":
                setPrice(fwjPriceList, yxProductInfos, i);
                break;
        }
    }

    private void setPrice(List<Double> PriceList, List<YxProProductInfo> yxProductInfos, int i) {
        if (yxProductInfos.get(i).getNum() > 1){
            //取均价；
            String averagePrice = String.format("%.2f", yxProductInfos.get(i).getNum() / yxProductInfos.get(i).getSum());
            PriceList.set(yxProductInfos.get(i).getMonth()-1,Double.parseDouble(averagePrice));
        }else {
            PriceList.set(yxProductInfos.get(i).getMonth()-1,Double.parseDouble(String.valueOf(yxProductInfos.get(i).getSum())));
        }
    }


}
